package a07_二叉树;

/**
 * <p>
 * a17_路径总和复习2
 * </p>
 *
 * @author flyduck
 * @since 2025/2/17
 */
public class a17_路径总和复习2 {

    int sum = 0;

    public boolean hasPathSum(TreeNode root, int targetSum) {
        if (root == null) {
            return false;
        }

        sum += root.val;

        if(root.left == null && root.right == null && sum == targetSum){
            return true;
        }

        if(root.left != null){
            boolean leftFlag = hasPathSum(root.left, targetSum);
            if (leftFlag) {
                return true;
            }else {
                sum -= root.left.val;
            }
        }

        if(root.right != null){
            boolean rightFlag = hasPathSum(root.right, targetSum);
            if (rightFlag) {
                return true;
            }else {
                sum -= root.right.val;
            }
        }

        return false;


    }


}
